//
//  CommonPickerViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-6-30.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "CommonPickerViewController.h"

@interface CommonPickerViewController ()

@property (weak, nonatomic) IBOutlet UIPickerView *picker;

@property (nonatomic, assign) int selectedRow;
@property (nonatomic, strong) NSMutableDictionary *selectedItemDict;

- (IBAction)dismissPickerAction:(id)sender;
- (IBAction)cancelAction:(id)sender;
- (IBAction)confirmAction:(id)sender;

@end

@implementation CommonPickerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.selectedItemDict = [[NSMutableDictionary alloc] init];
    self.selectedRow = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadData {
    [self.picker reloadAllComponents];
}

- (IBAction)dismissPickerAction:(id)sender {
    [self.view removeFromSuperview];
    if ([self parentViewController] != nil) {
        [self removeFromParentViewController];
    }
}

- (IBAction)cancelAction:(id)sender {
    [self dismissPickerAction:nil];
}

- (IBAction)confirmAction:(id)sender {
    [self dismissPickerAction:nil];
   
    if (self.delegate && [self.delegate respondsToSelector:@selector(picker:didSelectedItemIndexSet:)]) {
        
        [self.delegate picker:self didSelectedItemIndexSet:self.selectedItemDict];
    }
}

#pragma mark - UIPickerViewDataSource<NSObject>
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return [self.datas count];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if ([self.datas count] > component) {
        NSArray *componentData = [self.datas objectAtIndex:component];
        return [componentData count];
    }
    return 0;
}

#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 47;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *strTitle = @"";
    if ([self.datas count] > component) {
        NSArray *componentData = [self.datas objectAtIndex:component];
        if ([componentData count] > row) {
            strTitle = [componentData objectAtIndex:row];
        }
        return strTitle;
        
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//    self.selectedRow = row;
    [self.selectedItemDict setObject:[NSNumber numberWithInt:row] forKey:[NSNumber numberWithInt:component]];
}


@end
